package fi.iki.joker.mobilogger;

import android.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import fi.iki.joker.mobilogger.db.BlogConfigBean;
import fi.iki.joker.mobilogger.db.DBUtil;
import fi.iki.joker.mobilogger.db.ImageRepoBean;
import fi.iki.joker.mobilogger.util.AlertUtil;

/**
 * Lists the image repository configurations that are stored locally 
 * and allows user to edit or delete them.
 * 
 * @author juhak
 *
 */

public class ListImageRepoConfigs extends Activity implements 
	AdapterView.OnItemSelectedListener,
	AdapterView.OnItemClickListener {
	
	private DBUtil db = null;
	private String [] listData = null;
	private int currentlySelectedItem = -1;
	private final String TAG = "ListImageRepoConfigs";
	private ListView list = null;
	
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(fi.iki.joker.mobilogger.R.layout.managedata);
        TextView tv = (TextView) findViewById(fi.iki.joker.mobilogger.R.id.selected_button_view);
        tv.setText("Please select a image repository configuration from the list");
        ButtonBar.createButtons(this);
        ButtonBar.requestFocusForButton(ButtonBar.BUTTON_IMAGE_REPOS);
        list = (ListView)findViewById(fi.iki.joker.mobilogger.R.id.shown_data_list);
        updateListView(list);
        list.setOnItemSelectedListener(this);
        list.setOnItemClickListener(this);
	}
	
	private void updateListView(ListView list) {
		if(db == null) {
        	db = new DBUtil();
        }
        ImageRepoBean[] tempArr = db.getImageRepositories(this); 
        if(tempArr != null) {
        	list.setVisibility(View.VISIBLE);
        	listData = new String[tempArr.length];
        	for(int i = 0; i < tempArr.length; i++) {
        		ImageRepoBean bc = tempArr[i];
        		if(bc == null) {
        			listData[i] = "null";
        		} else {
        			listData[i] = new String(bc.toString());
        		}
        	}
        	list.setAdapter(new ArrayAdapter<String>(this, 
        			android.R.layout.simple_list_item_1,listData));
        } else {
        	list.setVisibility(View.INVISIBLE);
        }
               
	}

	public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		this.currentlySelectedItem = arg2;
	}

	public void onNothingSelected(AdapterView arg0) {
		this.currentlySelectedItem = -1;
	}
	
	public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
		this.currentlySelectedItem = arg2;
		// Think this through!
		AlertUtil.showAlert(this,"Select operation","What do you want to do?", "Edit", 
				mEditListener, "Delete", mDeleteListener);
	}

	
	OnClickListener mEditListener = new OnClickListener() {
    public void onClick(DialogInterface df, int item) {
        editEntry(getEntryId(listData[currentlySelectedItem]));
    	}
	};
	
    OnClickListener mDeleteListener = new OnClickListener() {
        public void onClick(DialogInterface df, int item) {
            deleteEntry(getEntryId(listData[currentlySelectedItem]));
        }
    };
	
    private void editEntry(int entryId) {
    	Intent intent = new Intent();
		intent.setClass(this, ImageRepoConfigEditor.class);
		intent.setAction(Intent.ACTION_EDIT);
		String URIStr = "content://fi.iki.joker.mobilogger/imagerepoconfig/"+entryId;
		intent.setData(Uri.parse(URIStr));
		this.startActivity(intent);
		this.finish();
    }
    
    private void deleteEntry(int entryId) {
     	db.deleteImageRepositoryItem(this,entryId);
    	updateListView(list);	
    }
    
    /**
     * This is ugly, we get the id from the [num]-Title,
     * should probably be done otherwise but this is simple
     */
    
    private int getEntryId(String configTitle) {
    	if(configTitle == null) return -1;
    	int start = 1; // the [
    	int end = configTitle.indexOf(']');
    	String number = configTitle.substring(start, end);
    	int res = -1;
    	try {
    		res = Integer.parseInt(number);
    	} catch (NumberFormatException e) {
    		Log.e(TAG,"Error parsing entry ID out of String: "+configTitle+" Start: "+start+" End: "+end);
    		return -1;
    	}
    	return res; 
    }
}
